package org.jeecg.modules.paycenter.channel.bankdocking.service;

import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.paycenter.channel.bankdocking.dto.Acc;
import org.jeecg.modules.paycenter.enums.PayChannelEnum;
import org.jeecg.modules.paycenter.service.BaseMemberService;
import org.jeecg.modules.paycenter.service.IMemberChannelService;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customer.entity.HyCustomerAccountBook;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerAccountBookService;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.hyplatform.entity.HyPlatformAccountBook;
import org.jeecg.modules.wlhy.hyplatform.service.IHyPlatformAccountBookService;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class NhMemberChannelService extends BaseMemberService implements IMemberChannelService {

    @Autowired
    private IHyCustomerAccountBookService hyCustomerAccountBookService;

    @Autowired
    private IHyPlatformAccountBookService hyPlatformAccountBookService;

    //region base
    @Override
    public PayChannelEnum getChannelEnum() {
        return PayChannelEnum.NH;
    }

    @Override
    public String getChannel() {
        return PayChannelEnum.NH.channelType;
    }

    @Override
    public boolean checkChannel() {
        HyConfig config = HyConfigUtils.getConfig();
        return config.getNhbankIzUse();
    }
    //endregion

    //region 账户相关

    @Override
    public BigDecimal getAvailBal() {
        return null;
    }

    @Override
    public Result<?> getCustomerAvailBalStr(HyCustomer customer) {
        HyCustomerAccountBook customerAccountBook = hyCustomerAccountBookService.getAccountBookByCustomerId(customer.getId());
        if (customerAccountBook == null) {
            return Result.error("尚未开通钱包功能!");
        }

        //检查货主运费是否够支付
        Acc customerAcc = hyCustomerAccountBookService.getCustomerAcc(customerAccountBook);
        if (customerAcc == null) {
            return Result.error("查询余额失败");
        }
        //可用余额
        BigDecimal availBal = customerAcc.getAvailBal();
        return Result.OK(availBal);
    }

    @Override
    public Result<?> getPlateformAvailBalStr() {
        //验证支付
        HyPlatformAccountBook platformAccountBook = hyPlatformAccountBookService.getAccountBook();
        if (platformAccountBook == null) {
            return Result.error("尚未开通平台钱包功能!");
        }

        //检查是否够支付
        Acc platformAcc = hyPlatformAccountBookService.getAcc(platformAccountBook);
        if (platformAcc == null) {
            return Result.error("查询平台钱包余额失败");
        }
        //可用余额
        BigDecimal availBal = platformAcc.getAvailBal();
        return Result.OK(availBal);
    }
    //endregion
}
